#设置各种参数
# typeset ORA_PRI_IP='10.27.5.199'               #主服务器ip
# typeset ORA_SLA_IP='10.27.5.139'               #从服务器ip
#typeset ORA_PRI_TNS_PORT='@@ORA_PRI_TNS_PORT@@'
#typeset ORA_SLA_TNS_PORT='@@ORA_SLA_TNS_PORT@@'
# typeset ORA_PORT='@@ORA_PORT@@'
# typeset hmuser='oracle'                   #主服务器远程用户
# typeset hmpassword='oracle'           #主服务器远程密码
# typeset hsuser='oracle'                   #从服务器远程用户
# typeset hspassword='oracle'           #从服务器远程密码
# typeset ORA_PRI_SID='orcl'                       #设置主库SID
# typeset ORA_SID_UP=$(echo "${ORA_SID}" | tr '[a-z]' '[A-Z]') #ORCL
# typeset ORA_SID_DOWN=$(echo "${ORA_SID}" | tr '[A-Z]' '[a-z]')
#typeset ORA_SLA_SID='orcl'                       #设置从库SID
# typeset ORA_PRI_DB_UNIQUE_NAME='@@ORA_PRI_DB_UNIQUE_NAME@@' #orcl_pri
# typeset ORA_SLA_DB_UNIQUE_NAME='@@ORA_SLA_DB_UNIQUE_NAME@@' #orcl_stl
#typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'            #设置从库uniqname
# typeset ORA_PRI_SERVICE_NAME='@@ORA_PRI_SERVICE_NAME@@' #同orcl_pri
# typeset ORA_SLA_SERVICE_NAME='@@ORA_SLA_SERVICE_NAME@@' #同orcl_stl
# typeset ORA_PRI_SERVICE_NAME='orcl_pri'
# typeset ORA_SLA_SERVICE_NAME='orcl_stl'
# typeset ORA_PRI_DB_UNIQUE_NAME_UP=$(echo "${ORA_PRI_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_PRI
# typeset ORA_SLA_DB_UNIQUE_NAME_UP=$(echo "${ORA_SLA_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_STL
# typeset ORA_PRI_SERVICE_NAME_UP="${ORA_PRI_DB_UNIQUE_NAME_UP}" #同ORCL_PRI
# typeset ORA_SLA_SERVICE_NAME_UP="${ORA_SLA_DB_UNIQUE_NAME_UP}" #同ORCL_STL
# typeset ORA_USER='@@ORA_USER@@' #sys
# typeset ORA_PASSWD='@@ORA_PASSWD@@' #sys
# typeset ORA_ROLE='@@ORA_ROLE@@' # 分开写，则为常量primary或者standby
# typeset ORA_BAK_HOME='@@ORA_BAK_HOME@@' 
# ORACLE_BASE=/db/app/oracle
# ORACLE_HOME=${ORACLE_BASE}/product/11.2.0/db_1
# SPFILE_PATH=${ORACLE_HOME}/dbs
# ORA_NLS10=${ORACLE_HOME}/nls/data
# TNS_ADMIN=${ORACLE_HOME}/network/admin

### test begin ###
typeset check_times=''                  #命令重复次数
typeset ORA_PRI_DB_UNIQUE_NAME='orcl_pri'            #设置主库uniqname
typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl02'          #slave2的uniqname
typeset ORA_SLA_SID='orcl'                    #slave2的sid
# typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'          #slave1的uniqname
# typeset ORA_SLA_SID='orcl'                    #slave1的sid

#配置日志路径
if [ -z "$ORA_SLA_IP" ];then
    $ORA_PRI_IP=$(ip a|grep `ip r|grep default|awk '{print $5}'`|grep inet|awk '{print $2}'|awk -F'/' '{print $1}')
fi
typeset SCRIPT_NAME="oracle_slave_${ORA_SLA_IP}"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

if [ ! -f "${LOG_FILE}" ];then
    touch ${LOG_FILE}
    if [ $? -ne 0 ];then
        log_format "ERROR" "failed to create log file" && exit 1
    else
        log_format "INFO" "success to create log file"
    fi
fi

log_format "begin to build oracle slave................................."

#设置oracle环境变量
source ~/.bash_profile

#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer';exit 1
fi

#备库创建静态监听启动
log_format 'INFO' "Create a static listener to start"
initfilepath=${ORACLE_HOME}/dbs/init${ORA_SLA_SID}.ora
if [ -e "$initfilepath.`date +%F`_shellbak" ];then
    log_format "INFO" "$initfilepath.`date +%F`_shellbak exits "
else
    cp $initfilepath{,.`date +%F`_shellbak}
    if [ -e "$initfilepath.`date +%F`_shellbak" ];then
        log_format "INFO" "$initfilepath.`date +%F`_shellbak create success"
    else
        log_format "ERROR" "$initfilepath.`date +%F`_shellbak faile to create" && exit 1
    fi
fi

##检查并修改DB_UNIQUE_NAME
cat $initfilepath|grep -v ^#|grep DB_UNIQUE_NAME > /dev/null
if [ $? -ne 0 ];then
    log_format 'ERROR' "initfile has no DB_UNIQUE_NAME ,please check!" && exit 1
else
    sed -i -e s/^DB_UNIQUE_NAME.*/DB_UNIQUE_NAME=${ORA_SLA_DB_UNIQUE_NAME}/ -e s/^\*\.DB_UNIQUE_NAME.*/\*\.DB_UNIQUE_NAME=\'${ORA_SLA_DB_UNIQUE_NAME}\'/ $initfilepath
    sed -n -e "/^DB_UNIQUE_NAME/p" -e "/^\*\.DB_UNIQUE_NAME/p" $initfilepath|grep -w "${ORA_SLA_DB_UNIQUE_NAME}"
    if [ $? -ne 0 ];then
        log_format 'ERROR' "db DB_UNIQUE_NAME change failed" && exit 1
    else
        log_format 'INFO' "db DB_UNIQUE_NAME change success"
    fi
fi


##检查或创建归档目录,有目录判断目录权限
if [ ! -d "${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog" ];then
  mkdir -p ${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog
  if [ $? -ne 0 ];then
    log_format "ERROR" "create dir ${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog failed."
    exit 10
  fi
else
    touch ${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog/`date +%F`.test.txt
    if [ $? -eq 0 ];then
        rm -rf ${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog/`date +%F`.test.txt
        log_format "INFO" "${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog have write permission"
    else
        log_format 'ERROR' "${ORACLE_BASE}/oradata/${ORA_SLA_SID}/archivelog no write permission" && exit 1
    fi
fi

#修改location的目录
localnum=`cat $initfilepath |grep -n LOCATION|awk -F':' '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
expr 1 + ${localnum} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" "can not find local DB_UNIQUE_NAME in ${initfilepath}" && exit 1
fi
sed -i s"%LOCATION.*%LOCATION=${ORACLE_BASE}\/oradata\/${ORA_SLA_SID}\/archivelog%" ${initfilepath}
lonum=`grep "LOCATION=${ORACLE_BASE}\/oradata\/${ORA_SLA_SID}\/archivelog" ${initfilepath}|grep -v grep |wc -l`
if [ $lonum -eq 0 ];then
    log_format "ERROR" "faile to change location path" && exit 1
else
    log_format "INFO" "success to change location path"
fi

##修改本地归档的uniqname
localnum=`cat $initfilepath |grep -n LOCATION|awk -F':' '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
expr 1 + ${localnum} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" "can not find local DB_UNIQUE_NAME in ${initfilepath}" && exit 1
fi
sed -i $(($localnum+2))s"/DB_UNIQUE_NAME.*/DB_UNIQUE_NAME=${ORA_SLA_DB_UNIQUE_NAME}\'/" ${initfilepath}
sed -n "$(($localnum+2))p" $initfilepath|grep -w "DB_UNIQUE_NAME=${ORA_SLA_DB_UNIQUE_NAME}" > /dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" "change local DB_UNIQUE_NAME failed" && exit 1
else
    log_format "INFO" "change local DB_UNIQUE_NAME success"
fi

#获取本机log_archive_dest的数字
localnum=0
for i in `cat $initfilepath|grep "LOG_ARCHIVE_DEST_[0-9][0-9]*"|awk -F '=' '{print $1}'|awk -F'_' '{print $4}'|sed s'/=//'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
do
    expr 1 + ${i} &>/dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" 'faile to get destnumber';exit 1
    fi
    if [ $i -gt $localnum ];then
        let localnum=i
    fi
done

##修改LOG_ARCHIVE_DEST_$localnum中主库的监听名
if [ `cat  $initfilepath|grep LOG_ARCHIVE_DEST_$localnum|grep -v grep |wc -l` -ne 1 ];then
    log_format "ERROR" "fail to find LOG_ARCHIVE_DEST_$localnum in $initfilepath" && exit 1
fi

if [ `cat  $initfilepath|grep LOG_ARCHIVE_DEST_$localnum|grep '\*\.'|grep -v grep |wc -l` -eq 1 ];then
    dest_localnum=`cat $initfilepath |grep -n "LOG_ARCHIVE_DEST_$localnum"|awk -F':' '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    expr 1 + ${dest_localnum} &>/dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "can not find LOG_ARCHIVE_DEST_$localnum information in ${initfilepath}" && exit 1
    fi
    sed -i $(($dest_localnum))s"/SERVICE.*/SERVICE=${ORA_PRI_DB_UNIQUE_NAME} ASYNC/" ${initfilepath}
    sed -n "$(($dest_localnum))p" $initfilepath|grep -w "SERVICE=${ORA_PRI_DB_UNIQUE_NAME} ASYNC" > /dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "change dest$localnum SERVICE failed" && exit 1
    else
        log_format "INFO" "change dest$localnum SERVICE success"
    fi

    ##修改dest$localnum中主库的db_uniq_name
    sed -i $(($dest_localnum+2))s"/DB_UNIQUE_NAME.*/DB_UNIQUE_NAME=${ORA_PRI_DB_UNIQUE_NAME}\'/" ${initfilepath}
    sed -n "$(($dest_localnum+2))p" $initfilepath|grep -w "DB_UNIQUE_NAME=${ORA_PRI_DB_UNIQUE_NAME}" > /dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "change dest$localnum db_uniq_name failed" && exit 1
    else
        log_format "INFO" "change dest$localnum db_uniq_name success"
    fi
else
    dest_localnum=`cat $initfilepath |grep -n "LOG_ARCHIVE_DEST_$localnum"|awk -F':' '{print $1}'| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    expr 1 + ${dest_localnum} &>/dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "can not find LOG_ARCHIVE_DEST_$localnum information in ${initfilepath}" && exit 1
    fi
    sed -i $(($dest_localnum+1))s"/SERVICE.*/SERVICE=${ORA_PRI_DB_UNIQUE_NAME} ASYNC/" ${initfilepath}
    sed -n "$(($dest_localnum+1))p" $initfilepath|grep -w "SERVICE=${ORA_PRI_DB_UNIQUE_NAME} ASYNC" > /dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "change dest$localnum SERVICE failed" && exit 1
    else
        log_format "INFO" "change dest$localnum SERVICE success"
    fi

    ##修改dest$localnum中主库的db_uniq_name
    sed -i $(($dest_localnum+3))s"/DB_UNIQUE_NAME.*/DB_UNIQUE_NAME=${ORA_PRI_DB_UNIQUE_NAME}\'/" ${initfilepath}
    sed -n "$(($dest_localnum+3))p" $initfilepath|grep -w "DB_UNIQUE_NAME=${ORA_PRI_DB_UNIQUE_NAME}" > /dev/null
    if [ $? -ne 0 ];then
        log_format "ERROR" "change dest$localnum db_uniq_name failed" && exit 1
    else
        log_format "INFO" "change dest$localnum db_uniq_name success"
    fi
fi


##修改FAL_SERVER为主库的监听名
sed -i s"/FAL_SERVER.*/FAL_SERVER=${ORA_PRI_DB_UNIQUE_NAME}/" $initfilepath
sed -n "/FAL_SERVER/p" $initfilepath|grep -w "FAL_SERVER=${ORA_PRI_DB_UNIQUE_NAME}" > /dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" "change FAL_SERVER name failed" && exit 1
else
    log_format "INFO" "change FAL_SERVER name success"
fi

##检查或创建相应目录
if [ ! -d "$ORACLE_BASE/admin/${ORA_SLA_SID}/adump" ];then
    mkdir -p $ORACLE_BASE/admin/${ORA_SLA_SID}/adump
    if [ $? -ne 0 ];then
        log_format "ERROR" "fail to mkdir $ORACLE_BASE/admin/${ORA_SLA_SID}/adump" && exit 1
    fi
fi

if [ ! -d "$ORACLE_BASE/oradata/${ORA_SLA_SID}" ];then
    mkdir -p $ORACLE_BASE/oradata/${ORA_SLA_SID}
    if [ $? -ne 0 ];then
        log_format "ERROR" "fail to mkdir $ORACLE_BASE/oradata/${ORA_SLA_SID}" && exit 1
    fi
fi

if [ ! -d "$ORACLE_BASE/oradata/${ORA_SLA_SID}/archivelog" ];then
    mkdir -p $ORACLE_BASE/oradata/${ORA_SLA_SID}/archivelog
    if [ $? -ne 0 ];then
        log_format "ERROR" "fail to mkdir $ORACLE_BASE/oradata/${ORA_SLA_SID}/archivelog" && exit 1
    fi
fi

log_format "INFO" "$ORACLE_BASE/admin/${ORA_SLA_SID}/adump,$ORACLE_BASE/oradata/${ORA_SLA_SID},$ORACLE_BASE/oradata/${ORA_SLA_SID}/archivelog exits"

#备库通过pfile生成spfile并启动数据库至nomount
spnum=$(sqlplus / as sysdba <<eof | grep "File created"|grep -v grep |wc -l
shutdown immediate;
create spfile from pfile;
eof
)
if [ $spnum -ne 1 ];then
    log_format "ERROR" "fail to create spfile from pfile" && exit 1
else
    log_format "INFO" "success to create spfile from pfile"
fi


nomountnum=$(sqlplus / as sysdba <<eof | grep "ORACLE instance started"|grep -v grep |wc -l
startup nomount;
eof
)
if [ $nomountnum -ne 1 ];then
    log_format "ERROR" "fail to start nomount" && exit 1
else
    log_format "INFO" "success to start nomount"
fi

lsnrctl stop
if [ $? -ne 0 ];then
    log_format "ERROR" "lsnrctl stop failed." && exit 1
else
    log_format "INFO" "lsnrctl stop successfully."
fi

lsnrctl start
if [ $? -ne 0 ];then
    log_format "ERROR" "lsnrctl start failed." && exit 1
else
    log_format "INFO" "lsnrctl start successfully."
fi